var t = require("../../utils/config"), o = require("../../utils/request"), e = require("../../utils/api"), n = require("../../utils/util");

require("../../utils/storage");

Page({
    data: {
        query: "",
        q: "",
        scancode_time: "",
        orderno: "",
        orderstatus: "",
        productInfo: "瑞慈医疗自费项目",
        systemid: t.systemId,
        amount: "0.00",
        companyName: "瑞慈体检",
        wxpayIcon: t.resourceRoot + "wxpay.png",
        wxpaycheckedIcon: t.resourceRoot + "checked-rich-green.png",
        showPayBtn: !1,
        openid: "",
        prePayPara: {},
        debug: ""
    },
    onLoad: function(t) {
        var o = this, e = decodeURIComponent(t.q), a = n.getUrlQueryParameters(e);
        if (a != {} && a.orderno) {
            this.setData({
                orderno: a.orderno
            });
            var i = this.getOpenId(), s = this.initPay();
            Promise.all([ i, s ]).then(function(t) {
                console.log("两个异步都执行完成--成功"), console.log(t), o.setData({
                    showPayBtn: parseFloat(o.data.amount) > 0 && "" != o.data.openid
                });
            }).catch(function(t) {
                console.log("两个异步都执行完成--抓到失败"), console.log(t), o.setData({
                    showPayBtn: !1
                });
            });
        } else wx.showToast({
            title: "订单号不正确!",
            icon: "error",
            duration: 2e3
        });
    },
    onPullDownRefresh: function() {},
    initPay: function() {
        var t = this;
        return wx.showLoading({
            title: "请稍候"
        }), new Promise(function(n, a) {
            o.request(o.HttpMethod.POST, e.apiRoute.getPayOrderInfo, {
                Systemid: t.data.systemid,
                outTradeNo: t.data.orderno
            }).then(function(o) {
                o.success && o.data.amount && t.setData({
                    amount: o.data.amount.toFixed(2)
                }), o.success || wx.showToast({
                    title: "获取订单失败!",
                    icon: "error",
                    duration: 2e3
                }), wx.hideLoading(), n(o);
            }).catch(function() {
                wx.hideLoading(), wx.showToast({
                    title: "获取订单异常!",
                    icon: "error",
                    duration: 2e3
                }), a();
            });
        });
    },
    doPay: function() {
        var t = this;
        wx.showLoading({
            title: "支付处理中"
        });
        var o = null, e = t.getJssdkSignature(), n = t.callPrePay().then(function(t) {
            o = t;
        });
        Promise.all([ e, n ]).then(function(e) {
            console.log("获取签名+预支付，两个异步都执行完成--成功"), console.log(e), null != o && t.callPay(o);
        }).catch(function(t) {
            console.log("获取签名+预支付，两个异步都执行完成--抓到失败"), console.log(t);
        });
    },
    getOpenId: function() {
        var t = this;
        return new Promise(function(n, a) {
            wx.login({
                success: function(a) {
                    console.log("获取连接时的login code：", a.code), o.request(o.HttpMethod.GET, e.apiRoute.getOpenid, {
                        code: a.code,
                        systemid: t.data.systemid
                    }).then(function(o) {
                        var e, a;
                        console.log(o.data), t.setData({
                            openid: null !== (e = null == o ? void 0 : o.data.openid) && void 0 !== e ? e : ""
                        }), n(null !== (a = null == o ? void 0 : o.data) && void 0 !== a ? a : "");
                    });
                },
                fail: function(t) {
                    console.log("获取连接时的login code err：", t), a("");
                }
            });
        });
    },
    getJssdkSignature: function() {
        var t = this, n = this;
        return wx.showLoading({
            title: "签名验证中"
        }), new Promise(function(a, i) {
            o.request(o.HttpMethod.POST, e.apiRoute.getJssdkSignature, {
                urlStr: "https://api.rich-healthcare.com/payment/payment.html?orderno=" + n.data.orderno + "&client=JSAPI&openid=" + n.data.openid + "&systemid=" + n.data.systemid,
                systemid: n.data.systemid
            }).then(function(t) {
                "20000" == t.resultCode ? a(t.data) : wx.showToast({
                    title: "签名获取失败!",
                    icon: "error",
                    duration: 2e3
                }), wx.hideLoading();
            }).catch(function() {
                wx.showToast({
                    title: "签名获取异常!",
                    icon: "error",
                    duration: 2e3
                }), t.setData({
                    btnConfirmLoading: !1
                }), wx.hideLoading(), i();
            });
        });
    },
    callPrePay: function() {
        var t = this, n = this;
        return wx.showLoading({
            title: "预支付"
        }), new Promise(function(a, i) {
            o.request(o.HttpMethod.POST, e.apiRoute.prePay, {
                SystemId: n.data.systemid,
                OutTradeNo: n.data.orderno,
                Body: n.data.productInfo,
                TotalFee: parseFloat(n.data.amount),
                SpBillCreateIp: "",
                TradeType: "JSAPI",
                OpenId: n.data.openid,
                Attach: ""
            }).then(function(t) {
                console.log(t), "20000" == t.resultCode ? a(t.data) : (wx.showModal({
                    title: "提示",
                    content: t.message,
                    success: function(t) {}
                }), i()), wx.hideLoading();
            }).catch(function(o) {
                wx.showToast({
                    title: "预支付异常!",
                    icon: "error",
                    duration: 2e3
                }), t.setData({
                    btnConfirmLoading: !1
                }), wx.hideLoading(), i();
            });
        });
    },
    callPay: function(t) {
        wx.showLoading({
            title: "支付唤起"
        }), wx.requestPayment({
            timeStamp: t.timeStamp,
            nonceStr: t.nonceStr,
            package: t.package,
            signType: t.signType,
            paySign: t.paySign,
            success: function(t) {
                wx.showToast({
                    title: "付款成功",
                    icon: "success",
                    duration: 2e3
                }), console.log("付款成功");
            },
            fail: function(t) {
                wx.showToast({
                    title: "支付失败!",
                    icon: "error",
                    duration: 2e3
                });
            }
        });
    }
});